package com.zsk.gen;

import org.apache.velocity.VelocityContext;

/**
 * 生成entity
 * 
 * @author keke
 * @date 2020/07/30
 */
public class ServiceGen {

    GenConfig config;
    String tableName;
    String comment;

    public ServiceGen(String tableName, String comment, GenConfig config) {
        super();
        this.tableName = tableName;
        this.config = config;
        this.comment = comment;
    }

    public void gen() {

        String entityName = GenUtil.transUnderlineToHump(tableName, true);
        String packageName = config.getBasePackage();
        if (GenUtil.isNotEmpty(config.getModuleName())) {
            packageName += "." + config.getModuleName();
        }
        String repositoryClassPath =
            packageName + "." + config.getRepositoryPackage() + "." + entityName + "Repository";
        String entityClassPath = packageName + "." + config.getEntityPackage() + "." + entityName;
        String pageBeanClassPath = config.getBasePackage() + ".bean.PageBean";
        packageName += "." + config.getServicePackage();

        VelocityContext context = VelocityUtil.createContext();
        context.put("author", config.getAuthor());
        context.put("time", GenUtil.getYmd());
        context.put("package", packageName);
        context.put("packageImpl", packageName + ".impl");
        context.put("tableComment", comment);
        context.put("entityName", entityName);
        context.put("entityClassPath", entityClassPath);
        context.put("serviceItfName", "I" + entityName + "Service");
        context.put("serviceItfPath", packageName + "." + "I" + entityName + "Service");
        context.put("repositoryClassName", entityName + "Repository");
        context.put("repositoryClassPath", repositoryClassPath);
        context.put("pageBeanClassPath", pageBeanClassPath);

        String vmPath = "com/zsk/template/IService.vm";
        String targetPath = "src/main/java/" + packageName.replaceAll("\\.", "/") + "/I" + entityName + "Service.java";

        String vmImplPath = "com/zsk/template/ServiceImpl.vm";
        String implTargetPath =
            "src/main/java/" + packageName.replaceAll("\\.", "/") + "/impl/" + entityName + "ServiceImpl.java";

        if (config.isCoverCode() || !GenUtil.fileExist(targetPath)) {
            VelocityUtil.build(vmPath, targetPath, context);
        }
        if (config.isCoverCode() || !GenUtil.fileExist(implTargetPath)) {
            VelocityUtil.build(vmImplPath, implTargetPath, context);
        }
    }

}
